<!-- 阿米巴仓库渠道管理 -->
<template>
  <div>
    <div class="margin border_blue">
      <div slot="header" class="clearfix text-left color_blue padding-sm">
        <span class="margin-right-xl">阿米巴仓库渠道管理</span>
        <el-button-group>
          <el-button size="mini" type="primary" @click="deleteDepotAmoebaForm()"
            >删除</el-button
          >
          <el-button
            size="mini"
            type="primary"
            @click="exportDepotAmoebaPrice(1)"
            >导出所有公式</el-button
          >
          <el-button
            size="mini"
            type="primary"
            @click="batchUploadDepotAmoebaPrice()"
            >批量上传</el-button
          >
          <el-button
            type="primary"
            size="mini"
            @click="exportDepotAmoebaPrice(0)"
            >下载导入模板</el-button
          >
        </el-button-group>
      </div>
      <div class="padding">
        <table
          border="1"
          cellpadding="0"
          cellspacing="0"
          style="table-layout: fixed"
          class="table-data"
        >
          <thead>
            <th>物流方法</th>
            <template v-for="(item, index) in depotAmoebaSubjects">
              <th>
                {{ payNameMap[item.payId] }}
                <span v-if="item.storageLocationId">
                  -{{ storageLocationSelector[item.storageLocationId] }}
                </span>
                <span v-if="item.channelChargingRule">
                  -{{ chargingRuleSelector[item.channelChargingRule] }}
                </span>
              </th>
            </template>
          </thead>
          <tbody>
            <tr v-for="(m, i) in channelWayMap" class="el-table__row">
              <td>
                <span> {{ channelWayMap[i] }} </span>
              </td>
              <td v-for="(item1, index1) in depotAmoebaSubjects">
                <template
                  v-if="
                    depotAmoebaCalculateFormulaMap &&
                    depotAmoebaCalculateFormulaMap[`${i}_${item1.id}`]
                  "
                >
                  <span>
                    {{
                      depotAmoebaCalculateFormulaMap[i + "_" + item1.id].formula
                    }}
                  </span>
                </template>
              </td>
            </tr>
          </tbody>
        </table>
      </div>
    </div>
    <div>
      <el-form>
        <el-row>
          <el-col :span="13">
            <el-form-item label="" prop="startTime">
              <el-date-picker
                type="datetime"
                v-model="amoebaPriceChannelRecord.startTime"
                format="yyyy-MM-dd HH:mm:ss"
                value-format="yyyy-MM-dd HH:mm:ss"
                :style="{ width: '100%' }"
                placeholder="开始时间(带时分秒)"
                clearable
              ></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="-" prop="endTime" label-width="30px">
              <el-date-picker
                type="datetime"
                v-model="amoebaPriceChannelRecord.endTime"
                format="yyyy-MM-dd HH:mm:ss"
                value-format="yyyy-MM-dd HH:mm:ss"
                :style="{ width: '100%' }"
                placeholder="结束时间(带时分秒)"
                clearable
              ></el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <el-button-group>
      <el-button type="primary" size="mini" @click="mergeBillingTiming()"
        >测试合并计费规则按钮</el-button
      >
    </el-button-group>
    <AmoebaDiversCostUploadfile
      ref="AmoebaDiversCostUploadfile"
    ></AmoebaDiversCostUploadfile>
  </div>
</template>
<script>
import { ajax } from "@/store/yg/common/utils";
import * as constant from "@/store/yg/common/constant";
const AmoebaDiversCostUploadfile = () => import("./AmoebaDiversCostUploadfile");
export default {
  components: {
    AmoebaDiversCostUploadfile,
  },
  provide() {
    return {
      autoInitData: this.autoInitData,
    };
  },
  data() {
    return {
      amoebaPriceChannelRecord: {
        startTime: "",
        endTime: "",
      },
      checkVisible: false,
      labelVisble: false,
      checkVisibleeditVisble: false,
      editDataVisble: false,
      items: [
        {
          sectionPrice: "",
          sectionPrices: "",
        },
      ],
      sectionPrices: 0,
      checkForm: {},
      labelForm: {},
      editForm: {},
      editDataForm: {},
      depotAmoebaCostConfigs: [
        {
          amoebaSubjectId: "",
          costId: "",
          price: "",
        },
      ],
      payNameMap: {},
      configPayNameMap: {},
      depotAmoebaCostLabelingConfigs: [],
      depotAmoebaSubjects: [],
      storageLocationSelector: [],
      chargingRuleSelector: [],
      channelWayMap: {},
      depotAmoebaCalculateFormulaMap: {},
      depotAmoebaCostConfig: {
        amoebaSubjectId: "",
        costId: "",
        price: "",
        type: 3,
      },
      depotAmoebaCostConfig1: {},
    };
  },
  mounted() {
    this.autoInitData();
  },
  methods: {
    autoInitData() {
      ajax("manageAmoebaDiversCost", {})
        .then((res) => {
          if (res.code == 200) {
            var data = res.data;
            console.log(data);
            this.depotAmoebaCostConfigs = data.depotAmoebaCostConfigs;
            this.payNameMap = data.payNameMap;
            this.configPayNameMap = data.configPayNameMap;
            this.depotAmoebaCostLabelingConfigs =
              data.depotAmoebaCostLabelingConfigs;
            this.depotAmoebaSubjects = data.depotAmoebaSubjects;
            this.storageLocationSelector = data.storageLocationSelector;
            this.chargingRuleSelector = data.chargingRuleSelector;
            this.channelWayMap = data.channelWayMap;
            this.depotAmoebaCalculateFormulaMap =
              data.depotAmoebaCalculateFormulaMap;
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },
    mergeBillingTiming() {
      ajax(
        "mergeBillingTiming",
        {
          startTime: this.amoebaPriceChannelRecord.startTime,
          endTime: this.amoebaPriceChannelRecord.endTime,
        },
        {
          class: "com.tofba.app.client.amoeba.MergeBillingTimingClient",
        }
      ).then(
        (res) => {
          for (let i of res) {
            if (i.type == 0) {
              this.isClaimCustomers = true;
            }
          }
        },
        (error) => {
          console.log(error);
        }
      );
    },
    saveDepotAmoebaCostConfig() {
      ajax("saveDepotAmoebaCostConfig", this.depotAmoebaCostConfig)
        .then((res) => {
          if (res.code == 200) {
            var data = res.data;
            console.log(data);
            this.$message.success("操作成功");
            this.timeout = setTimeout(() => {
              window.location.reload(true);
            }, 1000 * Math.random());
          } else {
            this.$message.error(res.message);
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },
    checkSave() {},
    labelSave() {
      console.log(this.items);
      for (var i = 0; i < this.items.length; i++) {
        if (
          this.items[i].priceCost == null ||
          this.items[i].priceSection == ""
        ) {
          alert("请补全区间");
          return false;
        }
      }
      var priceSection_array = new Array();

      for (var i = 0; i < this.items.length; i++) {
        var cur = {};
        var priceSection = this.items[i].priceSection;
        var priceCost = this.items[i].priceCost;
        cur["priceSection"] = priceSection;
        cur["priceCost"] = priceCost;
        priceSection_array.push(cur);
      }

      var priceSectionDataStr = JSON.stringify(priceSection_array);
      this.depotAmoebaCostConfig1.priceSectionJson = priceSectionDataStr;
      this.depotAmoebaCostConfig1.type = 8;
      this.depotAmoebaCostConfig1.price = 0;
      ajax("saveDepotAmoebaCostConfig", this.depotAmoebaCostConfig1)
        .then((res) => {
          if (res.code == 200) {
            var data = res.data;
            console.log(data);
            this.$message.success("操作成功");
            this.timeout = setTimeout(() => {
              window.location.reload(true);
            }, 1000 * Math.random());
          } else {
            this.$message.error(res.message);
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },
    deleteDepotAmoebaForm() {
      this.$confirm("此操作是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          ajax("deleteDiversCostForm", {}).then(
            (res) => {
              console.log(res);
              if (res.code == 200) {
                this.$message.success("操作成功");
                this.autoInitData();
              } else {
                this.$message.error("操作失败");
              }
            },
            (error) => {
              console.log(error);
            }
          );
        })
        .catch(() => {});
    },
    exportDepotAmoebaPrice(i) {
      ajax("exportDiversCostPrice", { isNeedFormula: i })
        .then((res) => {
          if (res.path) {
            //helper.hide_loading();
            var fileName = res.path;
            if (fileName) {
              window.open(constant.FILE_SERVER + fileName);
            } else {
              qiao.bs.alert("导出失败");
            }
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },
    batchUploadDepotAmoebaPrice() {
      this.$refs.AmoebaDiversCostUploadfile.centerDialogVisible = true;
    },
    deleteDepotAmoebaCostConfigById(id) {
      ajax("deleteDepotAmoebaCostConfigById", { id: id })
        .then((res) => {
          if (res.code == 200) {
            this.$message.success("删除成功");
            this.timeout = setTimeout(() => {
              window.location.reload(true);
            }, 1000 * Math.random());
          } else {
            this.$message.error(res.message);
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },
    editSimpleLabelForm(id) {
      this.labelVisble = true;
      ajax("findDepotAmoebaCostConfigbyid", { id: id })
        .then((res) => {
          this.depotAmoebaCostConfig1 = res;
          this.depotAmoebaCostConfig1.amoebaSubjectId =
            res.amoebaSubjectId.toString();
          this.depotAmoebaCostConfig1.costId = res.costId.toString();
          if (this.depotAmoebaCostConfig1.priceSectionJson) {
            var jsonObj = JSON.parse(
              this.depotAmoebaCostConfig1.priceSectionJson
            );
            this.items = jsonObj;
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },
    editSimpleDialogForm(id) {
      this.checkVisible = true;
      ajax("findDepotAmoebaCostConfigbyid", { id: id })
        .then((res) => {
          this.depotAmoebaCostConfig = res;
          this.depotAmoebaCostConfig.amoebaSubjectId =
            res.amoebaSubjectId.toString();
          this.depotAmoebaCostConfig.costId = res.costId.toString();
        })
        .catch((error) => {
          console.log(error);
        });
    },
    edit() {
      this.editDataVisble = true;
    },
    onAdd(index) {
      this.items.push({ priceSection: "", priceCost: "" });
    },
    onDel(index) {
      this.items.splice(index, 1);
    },
  },
};
</script>
<style scoped>
.border {
  border: 1px solid #d6e9c6;
  border-radius: 5px;
}
.border_blue {
  border: 1px solid #bce8f1;
  border-radius: 5px;
}
.color {
  background-color: #dff0d8;
  color: #3c763d;
  background-image: linear-gradient(to bottom, #dff0d8 0, #d0e9c6 100%);
}
.color_blue {
  background-color: #d9edf7;
  color: #31708f;
  background-image: linear-gradient(to bottom, #d9edf7 0, #c4e3f3 100%);
}
/deep/.el-select {
  width: 100%;
}
.table-data {
  width: 100%;
  background: #fff;
  border-top: solid 1px #ddd;
  border-left: solid 1px #ddd;
  border-collapse: collapse;
  text-align: left;
}
.table-data td,
th {
  padding: 2px 4px;
  border-right: solid 1px #ddd;
  border-bottom: solid 1px #ddd;
}
</style>
